/*

*/


define(
   [
      'dojo/_base/declare',
      'dojo/_base/connect',
      'dojo/_base/lang'
   ],
   function(declare, connect, lang)
   {
      return declare([],
      {
         itemAttr: 'selectedItem',
         postscript:function (args)
         {
            this.inherited(arguments);
            if(!args.pTarget || !args.pMenu)
            {
               // We can't do anythign so just return
               return;
            }

            // Bind menu to target
            args.pMenu.bindDomNode(args.pTarget.domNode);

            this.setupMenuOnTarget(args.pMenu, args.pTarget);
         },
         setupMenuOnTarget: function(pMenu, pTarget)
         {
            // Set the target on the menu if it has not already been set
            if(pMenu.setTarget != undefined && !pMenu.mTarget)
               pMenu.setTarget(pTarget);

            // Set the menu to open on the context
            var itemAttr = this.itemAttr;
            dojo.connect(pMenu, "_openMyself", pTarget, lang.hitch(pMenu, function(e)
            {
               // Get a hold of the source of the context popup
               var pSource = dijit.getEnclosingWidget(e.target);

               // Remember the last item to have a menu come up
               this.mLastItemOpenedOn = pSource[itemAttr];

               this.menuOpened(e, pSource, pTarget);
            }));
         }
      });
   }
);
